﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using System.Diagnostics;
using NPOI.HSSF.Util;

namespace CreatDateTableByYear
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            //获取输入
            Console.WriteLine("年份日历表生成");
            Console.WriteLine("请输入起始年份：");
            int yearStart = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入结束年份：");
            int yearEnd = int.Parse(Console.ReadLine());
            Console.WriteLine("正在生成,请稍后......");

            //创建行
            string fileName = yearStart + "年日历表.xlsx";
            DateTime day = new DateTime(yearStart, 1, 1);
            IWorkbook workbook = new XSSFWorkbook();//创建工作簿
            ISheet sheet = workbook.CreateSheet("Sheet1");//创建表
            IRow rowYear = sheet.CreateRow(0);
            rowYear.CreateCell(0).SetCellValue("年份");
            IRow rowDay = sheet.CreateRow(1);
            rowDay.CreateCell(0).SetCellValue("日期");
            IRow rowWeek = sheet.CreateRow(2);
            rowWeek.CreateCell(0).SetCellValue("星期");
            IRow rowNongli = sheet.CreateRow(3);
            rowNongli.CreateCell(0).SetCellValue("农历");
            int cellNum = 1;

            //单元格式
            ICellStyle styleCenterNomo = workbook.CreateCellStyle();
            styleCenterNomo.Alignment = HorizontalAlignment.Center;
            //单元格式
            ICellStyle styleCenter = workbook.CreateCellStyle();
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.FillForegroundColor = HSSFColor.Green.Index + 4;
            styleCenter.FillPattern = FillPattern.SolidForeground;
            //单元格式
            ICellStyle stylWeekend = workbook.CreateCellStyle();
            stylWeekend.Alignment = HorizontalAlignment.Center;
            stylWeekend.FillForegroundColor = HSSFColor.Yellow.Index;
            stylWeekend.FillPattern = FillPattern.SolidForeground;

            int lastMonth = 1;
            int colorStep = 1;
            while (day.Year <= yearEnd)
            {
                String dayStr = day.ToString("MMdd");
                int weekStr = (int)day.DayOfWeek;
                if (day.Month != lastMonth)
                {
                    short color = (short)(styleCenter.FillForegroundColor + colorStep);
                    colorStep *= -1;
                    styleCenter = workbook.CreateCellStyle();
                    styleCenter.Alignment = HorizontalAlignment.Center;
                    styleCenter.FillForegroundColor = color;
                    styleCenter.FillPattern = FillPattern.SolidForeground;
                }
                //设置年份
                ICell cellYear = rowYear.CreateCell(cellNum);
                cellYear.SetCellValue(day.Year.ToString());
                cellYear.CellStyle = styleCenterNomo;

                //设置日期
                ICell cellDay = rowDay.CreateCell(cellNum);
                cellDay.SetCellValue(dayStr);
                cellDay.CellStyle = styleCenter;
                if (NongLiHellper.HolidaysDic.ContainsKey(dayStr))
                {
                    cellDay.CellStyle = stylWeekend;
                }
                //设置星期
                ICell cellWeek = rowWeek.CreateCell(cellNum);
                cellWeek.SetCellValue(weekStr);
                if (weekStr == 6 || weekStr == 0) { cellWeek.CellStyle = stylWeekend; } else { cellWeek.CellStyle = styleCenter; };//分别设置周末和工作日的颜色
                //设置农历
                ICell Cellnongli = rowNongli.CreateCell(cellNum);
                string nongLiDayStr = NongLiHellper.GetChineseDateTimeShort(day);
                Cellnongli.SetCellValue(nongLiDayStr);
                if (NongLiHellper.HolidaysDic.ContainsKey(nongLiDayStr))
                {
                    Cellnongli.CellStyle = stylWeekend;
                }
                lastMonth = day.Month;
                day = day.AddDays(1);
                cellNum++;
            }
            //sheet.CreateFreezePane(1,4);
            workbook.Write(File.Create(fileName));

            Console.WriteLine("生成完毕，按回车键打开文件");
            Console.WriteLine("提示:每年清明节非固定农历或阳历日期,故未在表格上以黄色背景标注!");
            Console.ReadLine();
            Process.Start(fileName);
        }
    }
}